package com.hj.dapp.business.engineer.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hj.dapp.business.engineer.mapper.EngineerProjectMapper;
import com.hj.dapp.business.engineer.model.EngineerProject;
import com.hj.dapp.business.engineer.service.EngineerProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

import static com.hj.dapp.common.constant.LightAuxiliaryConstant.DISCOUNT_RATE;
import static com.hj.dapp.common.constant.LightAuxiliaryConstant.MAIN_DISCOUNT_RATE;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2024/1/23
 */
@Service
public class EngineerProjectServiceImpl implements EngineerProjectService {
    @Autowired
    private EngineerProjectMapper engineerProjectMapper;

    public void save(EngineerProject engineerProject) {
        //设置默认折扣率
        if (ObjectUtils.isEmpty(engineerProject.getDiscountRate())) {
            engineerProject.setDiscountRate(DISCOUNT_RATE);
        }
        //设置主材折扣率
        if (ObjectUtils.isEmpty(engineerProject.getMainDiscountRate())) {
            engineerProject.setMainDiscountRate(MAIN_DISCOUNT_RATE);
        }
        engineerProjectMapper.insert(engineerProject);
    }

    @Override
    public List<EngineerProject> listByIntendedCustomer(Long intendedCustomerId) {
        QueryWrapper<EngineerProject> wrapper = new QueryWrapper<>();
        wrapper.eq("intended_customer_id", intendedCustomerId);
        return engineerProjectMapper.selectList(wrapper);
    }

    @Override
    public void deleteBatch(List<EngineerProject> engineerProjects) {
        if (!ObjectUtils.isEmpty(engineerProjects)) {
            engineerProjectMapper.
                    deleteBatchIds(engineerProjects.stream().map(EngineerProject::getId).collect(Collectors.toList()));
        }

    }

    @Override
    public void deleteIntendedCustomerProject(Long IntendedCustomerId) {
        QueryWrapper<EngineerProject> wrapper = new QueryWrapper();
        wrapper.eq("intended_customer_id", IntendedCustomerId);
        engineerProjectMapper.delete(wrapper);
    }

    @Override
    public List<EngineerProject> listEngineerProjectByCustomer(Long customerId) {
        QueryWrapper<EngineerProject> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id", customerId).or().eq("intended_customer_id", customerId);
        return engineerProjectMapper.selectList(wrapper);
    }

    @Override
    public EngineerProject get(Long engineerProjectId) {
        return engineerProjectMapper.selectById(engineerProjectId);
    }

    @Override
    public void setDiscountRate(EngineerProject engineerProject) {
        engineerProject.setDiscountRate(engineerProject.getDiscountRate());
        engineerProjectMapper.updateById(engineerProject);
    }

    @Override
    public void setMainDiscountRate(EngineerProject engineerProject) {
        engineerProject.setMainDiscountRate(engineerProject.getMainDiscountRate());
        engineerProjectMapper.updateById(engineerProject);
    }
}
